﻿namespace BEGDC.CC.Web.EZVisit.Common
{
    #region Using Directives

    using System;
    using ExcelTool;
    using Resource;
    using Utility.Extensions;
    using Workflow;

    #endregion

    public partial class ExcelExport : BasePage, IEZVisitPageAccess
    {
        #region IEZVisitPageAccess Members

        public EZVisitAccessControl AccessControl { get; set; }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request.QueryString["ID"].IsNotNullOrWhiteSpace())
            {
                try
                {
                    int id;

                    if (int.TryParse(this.Request.QueryString["ID"], out id) && this.CheckPermission(id))
                    {
                        switch (this.Request.QueryString["type"])
                        {
                            case "customer":
                                new ExportCustomers().Export(this, id);
                                break;
                            default:
                                new ExportEZVForm().Export(this, id);
                                break;
                        }
                    }
                }
                catch (Exception exception)
                {
                    this.Response.Write("<script type=\"text/javascript\">alert('" + ErrorMessage.Export_0001 + "');window.opener = null;window.close();</script>");
                }
            }
        }

        private bool CheckPermission(int appId)
        {
            this.AccessControl = new EZVisitAccessControl(this.UserInfo, appId);

            if (!this.AccessControl.HasViewPermission)
            {
                this.Response.Write("<script type=\"text/javascript\">alert('You were not granted the permission to view this page.\\n\\n您没有查看此页面的权限。');window.opener = null;window.close();</script>");
                return false;
            }

            return true;
        }
    }
}